
//加载公共样式
require('../../assets/css/index.less')
//axios
const request = require('../../utils/request')
//字体图标
require('../../assets/fonts/iconfont.css')

// weui
require('../../lib/weui/weui.css')
let weui = require('../../lib/weui/weui.js')

//服务器地址
const API = 'http://fitness.h5.itsource.cn:3701'
const shenfen = document.querySelector('#shenfen')
const chengshi = document.querySelector('#chengshi')
//加载自己的样式
require('./dynamic.less')
let dom = require('../../utils/dom.js')

//DOM加载事件
dom.ready(function () {
    //定义省份变量
    let proData;
    let cityId;
 //7)个人简介字符数处理
 dom.getEle('#info').addEventListener('input', function () {
    let len = this.value.length;
    if (len > 19) {
        this.value = this.value.slice(0,140);
    }
    dom.getEle('#num').innerHTML = this.value.length;

})

  //5)选择省份
  shenfen.addEventListener('click', function () {
    //1.发ajax获取省份数据并拼接weui所需格式
    request.get('api/shared/province', function (res) {
        //解构参数
        let { errno, data } = res;
        //判定
        if (errno == 0) {
            console.log(res);
            /*weuipicker格式为以下:
                [{
                    label: '男',
                    value: 0
                }]
            
            */
            //处理数据格式
            data = data.map(v => {
                console.log(v);
                return {
                    label: v.name,
                    value: v.id
                }
            })
            //2.调用weui选择器
            weui.picker(data, {
                // 选中触发事件
                onConfirm: function (result) {
                    //把选中的值添加到网页中
                    dom.getEle('#provinceVal').innerHTML = result[0].label
                    //保存省份ID
                    proData = result[0].value

                },
                title: '请选择省份'
            });

        } else {
            weui.alert(res.message)
        }
    })

})

//6)选择城市

cityVal.addEventListener('click', function () {
    //判定选择了省份再进行下面代码
    if (proData) {
        //1.发ajax获取城市数据并拼接weui所需格式
        request.get(`/api/shared/city?provinceId=${proData}`, function (res) {
            //解构参数
            let { errno, data } = res;
            //判定
            if (errno == 0) {
                /*weuipicker格式为以下:
                    [{
                        label: '男',
                        value: 0
                    }]
                
                */
                //处理数据格式
                data = data.map(v => {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                //2.调用weui选择器
                weui.picker(data, {
                    // 选中触发事件
                    onConfirm: function (result) {
                        //把选中的值添加到网页中
                        dom.getEle('#cityVal').innerHTML = result[0].label;
                        //保存城市id
                        cityId = result[0].value;

                    },
                    title: '请选择城市'
                });

            } else {
                weui.alert(res.message)
            }
        })
    }

})











})

